package pl.edu.pw.polygen.ui.project;

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

@Component("projectForm")
@Scope("prototype")
public class ProjectFormImpl extends GridLayout implements ProjectForm{

	private static final long serialVersionUID = 229575251353716766L;
	private static final String COMMON_FIELD_WIDTH = "40em";
	
	private static final int rowsAmount = 2;
	private static final int columnsAmount = 2;
	
	private TextField projectName;
	private TextArea projectDescription;
	
	public ProjectFormImpl(){
		super(columnsAmount,rowsAmount);
		setSpacing(true);
	}
	
	@SuppressWarnings("unused")
	@PostConstruct
	private void initialize(){
		addComponent(new Label("Name: "), 0, 0);
		addComponent(getProjectNameField(),1,0);
		addComponent(new Label("Description: "), 0, 1);
		addComponent(getProjectDescriptionField(),1,1);
	}
	
	private TextField getProjectNameField(){
		if(projectName==null){
			projectName = new TextField();
			projectName.setWidth(COMMON_FIELD_WIDTH);
			projectName.setRequired(true);
			projectName.setRequiredError("Please enter a project name");
		}
		return projectName;
	}
	
	private TextArea getProjectDescriptionField(){
		if(projectDescription==null){
			projectDescription = new TextArea();
			projectDescription.setWidth(COMMON_FIELD_WIDTH);
		}
		return projectDescription;
	}

	@Override
	public String getProjectName() {
		return (String) projectName.getValue();
	}

	@Override
	public void setProjectName(String name) {
		projectName.setValue(name);
	}

	@Override
	public String getProjectDescription() {
		return (String) projectDescription.getValue();
	}

	@Override
	public void setProjectDescription(String description) {
		projectDescription.setValue((description==null)? "":description);
	}

	@Override
	public boolean isValid() {
		projectName.commit();
		projectName.validate();
		return projectName.isValid();
	}
	
	
}
